<?php

namespace app\libs\orm;

abstract class SimpleORM
{
    protected static $pdo;

    protected static $convention = [
        'primary' => 'id',
        'foreign' => 'id_{table}'
    ];

    protected static $object = [
        'collection' => null,
        'entity' => null,
        'models' => null
    ];

    public static function pdo(\PDO $input = null)
    {
        if(empty(static::$object['collection']))
            static::$object['collection'] = '\\'. __NAMESPACE__ . '\Collection';

        if(empty(static::$object['entity']))
            static::$object['entity'] = '\\' . __NAMESPACE__ . '\Entity';

        return empty($input) ? static::$pdo : static::$pdo = $input;
    }

    public static function convention($key, $value = null)
    {
        return empty($value) ? static::$convention[$key] : static::$convention[$key] = $value;
    }

    public static function object($key, $value = null)
    {
        return empty($value) ? static::$object[$key] : static::$object[$key] = $value;
    }

    public static function get($table)
    {
        return static::__callStatic($table);
    }

    public static function __callStatic($method, $args = [])
    {
        if(method_exists(get_called_class(), $method))
            return forward_static_call_array($method, $args);
        else
        {
            $class = static::object('collection');
            $collection = new $class($method);
            return isset($args[0]) ? $collection[$args[0]] : $collection;
        }
    }
}